import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Viewer extends JLabel implements MouseListener 
{
    HardDisk hardDisk;
    
    public static final int OFFSET_X = 10;
    public static final int OFFSET_Y = 10;
    
    public boolean readBlock = false;
    
    public Viewer(HardDisk hardDisk)
    {
        this.hardDisk = hardDisk;
        this.addMouseListener(this);
    }
    public void paint(Graphics g)
    {
        int track = hardDisk.trackNumber;
        int sector = hardDisk.sectorNumber;
        
        for(int t=0;t<track;t++)
        {
            for(int s=0;s<sector;s++)
            {
                if(hardDisk.platter.surface[t][s]==0)
                    g.drawRect(OFFSET_X + s*50,OFFSET_Y+ t*50, 45, 45);
                else if(hardDisk.platter.surface[t][s]==1)
                    g.fillRect(OFFSET_X + s*50,OFFSET_Y+ t*50, 45, 45);
            }
        }
        
        int headTrack = hardDisk.headTrack;
        int headSector = hardDisk.headSector;
        
        if(readBlock)
        {
            readBlock = false;
            g.setColor(Color.green);
        }
        else
        {
            g.setColor(Color.red);
        }
        
        g.drawRect(OFFSET_X + headSector*50-2,OFFSET_Y + headTrack*50-2, 49, 49);
    }
    
    public void mouseExited(MouseEvent me)
    {
        
    }
    
    public void mouseEntered(MouseEvent me)
    {
        
    }
    
    public void mouseReleased(MouseEvent me)
    {
        
    }
    
    public void mousePressed(MouseEvent me)
    {
        int x = me.getX();
        int y = me.getY();
        
        int sector = (x-OFFSET_X)/50;
        int track = (y-OFFSET_Y)/50;
        
        System.out.println(track+" "+sector);
        
        hardDisk.addRequest(new Request(track, sector));
        repaint();
    }
    
    public void mouseClicked(MouseEvent me)
    {
        
    }
    
}